package components.google
{
	import UIFree.ObjectEvent;
	import UIFree.controls.FloatingArea.uiff_frame;
	import UIFree.controls.uif_input;
	import UIFree.controls.uif_label;
	import UIFree.controls.uif_scrollbar;
	import UIFree.uif_component;
	
	import UIUtils.uif_cache;
	
	import classes.clt_locale;
	import classes.clt_search;
	import classes.search.search_cache;
	
	import components.clt_arrowB;
	import components.clt_component;
	import components.clt_previewItem;
	import components.pj_hscrollArea;
	
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.TextFieldType;
	
	public class clt_searchLayer extends clt_component {
		private var _layer	: uiff_frame;
		private var _input	: uif_input;
		private var _button	: clt_arrowB;
		private var _filterList: Array;
		private var _scrollbar: uif_scrollbar;
		private var _cache: uif_cache;
		private var _dataInCache: Array;
		private var _wasScrolling: Boolean;
		public function clt_searchLayer(parent:uif_component, a_cache: uif_cache, ax:int=0, ay:int=0, awidth:int=0, aheight:int=0, styleClass:Object='') {
			super(parent, ax, ay, awidth, aheight, styleClass);
			_cache = a_cache;
		}
		
		override protected function createComponents():void {
			var inner: Rectangle = getInner();
			var tr: Rectangle = inner.clone();
			tr.height = 32;
			inner.top = tr.bottom;
				
			_button = new clt_arrowB(this, tr.right - 90, tr.y, 90, tr.height, clt_locale.SEARCHSEND, doSearch);
			var _label: uif_label = new uif_label(this, tr.x, tr.y, 60, tr.height, 'lbLabel');
			_label.text = clt_locale.SEARCHTEXT;
			
			tr.left += _label.width;
			tr.width -= _button.width;
			_input = new uif_input(this, tr.x, tr.y, tr.width, tr.height, app.styles.searchInput);
			_input.text = clt_locale.DEFAULTSEARCHTEXT; 
			_input.addEvent(KeyboardEvent.KEY_DOWN, doInputKeyDown, _input.textField);
			_input.addEvent(Event.CHANGE, doChange, _input.textField);
			
			_layer = new uiff_frame(this, inner.x, inner.y, inner.width, inner.height - 30, app.styles.searchFrame);
			_scrollbar = new uif_scrollbar(this, inner.x, _layer.getOuter().bottom, 
											inner.width, 30, 'scrollbar');
			_scrollbar.addEvent(uif_scrollbar.UPDATEVALUE, doScrolling);
			
			_layer.area.addEvent(uif_component.RESIZE, doAreaResize);
			_layer.area.addEvent(uif_component.POSITION, doAreaPosition);
			doCompleteSearch(search_cache.FLOWERS.items);
		}
		
		private function doScrolling(e: Event): void {
			_layer.scrollingValue = new Point(_scrollbar.value, 0);
			_wasScrolling = true;
		}
		
		private function doAreaResize(e: Event): void {
			_scrollbar.max = _layer.scrollingTotal.x;
		}
		
		private function doAreaPosition(e: Event): void {
			_scrollbar.value = _layer.scrollingValue.x;
		}
		
		private function doInputKeyDown(e: KeyboardEvent): void {
			if (e.charCode == 13) beginSearch();
		}
		
		private function doChange(e: Event): void {
			refreshFromCache();
		}
		
		private function refreshFromCache(): void {
			_dataInCache = _cache.getValue(_input.text) as Array;
			_button.text = _dataInCache?clt_locale.SEARCHCONTINUE:clt_locale.SEARCHSEND
		}
		
		private function doSearch(e: Event): void {
			if (!clt || clt.user.vipStatus) beginSearch();
			else clt.alert(clt_locale.WARNING, clt_locale.VIPUSERSONLY);
		}
		
		private function beginSearch(): void {
			var curText: String = _input.text;
			if (curText.replace(' ', '') == '') return;
			
			function l_doCompleteSearch(list: Array): void {
				_cache.setValue(curText, _dataInCache);
				doCompleteSearch(list);
				refreshFromCache();
			}
			if (_dataInCache) {
				if (!(_dataInCache[0] is Array)) _dataInCache = [_dataInCache];
				clt_search.search(curText, function(list: Array): void {
					if (list.length > 0) {
						_dataInCache.push(list);
						l_doCompleteSearch(list);
					} else clt.alert(clt_locale.WARNING, clt_locale.EMPTYSEARCHRESULT);
				}, 1 + _dataInCache.length * 10);
				//doCompleteSearch(_dataInCache);
			} else clt_search.search(curText, function(list: Array): void {
				_dataInCache = list;
				l_doCompleteSearch(list);
			});
		}
		
		protected function get rows(): int {
			return 2;
		}
		
		protected function doCompleteSearch(list: Array): void {
			var inner: Rectangle = _layer.maskBounds;
			var first: clt_searchItem;
			list.forEach(function(item: Object, i: int, arr: Array): void {
				var s_item: clt_searchItem = new clt_searchItem(_layer.area, item, _layer.area.nextXPos, 0, inner.height, inner.height, 'userPhoto');
				s_item.addEvent(MouseEvent.MOUSE_DOWN, doItemDown);
				s_item.addEvent(MouseEvent.CLICK, doItemClick);
				if (!first) first = s_item;
			});
			
			_layer.scrollToItem(first);
		}
		
		private function doItemDown(e: MouseEvent): void {
			_wasScrolling = false;
		}
		
		private function doItemClick(e: MouseEvent): void {
			if (!_wasScrolling) dispatchEvent(new ObjectEvent(Event.SELECT, (e.currentTarget as clt_searchItem)._data));
		}
	}
}